home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / KlingonBGApp.sit / Klingon BG App / Source / SoundUtils.c < prev    next >
Text File  |  1997-06-28  |  4KB  |  190 lines

  1. // Source code for Klingon Clock.   Copyright (C) 1996-1997
  2. // Charles H. Hemstreet IV
  3. //
  4. // Started at MacHack 1996
  5. // Completed at MacHack 1997
  6. //
  7. // Best thanks to:
  8. // My wife Regie, son Chad and baby
  9. // Other thanks to Elden Wood and Bob Clark
  10. //
  11. // This code is distributed "as-is" and implies no warranty or guarantee.
  12.  
  13.  
  14. #ifndef __SOUNDUTILS__
  15. #include "SoundUtils.h"
  16. #endif
  17.  
  18.  
  19.  
  20. RoutineDescriptor ExternalCallBackRD = 
  21.         BUILD_ROUTINE_DESCRIPTOR(uppFilePlayCompletionProcInfo, ExternalCallBack);
  22.  
  23. pascal void ExternalCallBack (SndChannelPtr theChannel, SndCommand theCommand)
  24. {
  25.     long        thisA5; 
  26.     long        wasA5;
  27.     
  28.     if (theCommand.param1 == kSoundDone)
  29.     {
  30.         wasA5 = theCommand.param2;
  31.         thisA5 = SetA5(wasA5);
  32.         
  33.         externalPriority = 0;
  34.         
  35.         thisA5 = SetA5(thisA5);
  36.     }
  37. }
  38.  
  39.  
  40.  
  41. OSErr openSndChannel(void)
  42. {
  43.     OSErr    theErr;
  44.     
  45.  
  46.     #if USESROUTINEDESCRIPTORS
  47.         externalCallBackUPP = &ExternalCallBackRD;
  48.     #else
  49.         externalCallBackUPP = (SndCallBackUPP) &ExternalCallBack;
  50.     #endif
  51.  
  52.  
  53.     theErr = noErr;
  54.     
  55.     if (gChannelOpen)
  56.         return theErr;
  57.     
  58.     gSndChannel = 0L;
  59.     theErr = SndNewChannel(&gSndChannel, sampledSynth, initNoInterp + initMono, 
  60.                             (SndCallBackUPP)externalCallBackUPP);
  61.     if (theErr == noErr)
  62.         gChannelOpen = true;
  63.     
  64.     return theErr;
  65.  
  66. OSErr closeSndChannel(void)
  67. {
  68.     OSErr        theErr;
  69.     
  70.     theErr = noErr;
  71.     
  72.     if (!gChannelOpen)
  73.         return theErr;
  74.     
  75.     if (gSndChannel != 0L)
  76.         theErr = SndDisposeChannel(gSndChannel, false);
  77.     gSndChannel = 0L;
  78.     
  79.     if (theErr == noErr)
  80.         gChannelOpen = false;
  81.     
  82.     return theErr;
  83.  
  84.  
  85. OSErr playASound (short soundID, short priority)
  86. {
  87.     SndCommand    theCommand;
  88.     OSErr        theErr;
  89.     
  90.     assert(gSndChannel != nil);
  91.  
  92.     externalPriority = priority;
  93.     
  94.     theCommand.cmd = bufferCmd;
  95.     theCommand.param1 = 0;
  96.     theCommand.param2 = (long)(gTheSoundData[soundID]);
  97.     theErr = SndDoCommand(gSndChannel, &theCommand, false);
  98.     
  99.     return theErr;
  100. }
  101.  
  102.  
  103. void doPlayASound (short soundID, short priority)
  104. // Remember! Pass as soundID the sound-number-in-file, NOT the ID!
  105. {
  106.     assert(soundID >= 0);
  107.     assert(soundID < kNumberSounds);
  108.     assert(gChannelOpen != false);
  109.     if (priority >= externalPriority) 
  110.     {
  111.         playASound(soundID, priority);
  112.     }
  113. }
  114.  
  115.  
  116. Boolean SndChanDone(SndChannelPtr chan) 
  117. {
  118. /*
  119.     Same as SndDone, but you can pass any 'ol sound channel,
  120.     not just our private array of snd channels...
  121. */
  122.     OSErr err;
  123.     SCStatus status;
  124.  
  125.     // Poll the channel
  126.     err = SndChannelStatus(chan, sizeof(SCStatus), &status);
  127.     if (err == noErr)
  128.         // If the channel is busy, then the sound is NOT done,
  129.         // else the sound is done (and the channel is idle)...
  130.         return status.scChannelBusy;
  131.     else
  132.         return err; // Hmm. Error!
  133. } // END SndChanDone
  134.  
  135.  
  136. OSErr LoadBufferSounds (void)
  137. {
  138.     Handle        theSound;
  139.     long        soundDataSize;
  140.     OSErr        theErr;
  141.     short        i;
  142.     
  143.     theErr = noErr;
  144.     
  145.     for (i = 0; i < kNumberSounds; i++)
  146.     {
  147.         theSound = GetResource('snd ', i + kFirstSoundID);
  148.         if (theSound == 0L)
  149.             return ResError();
  150.         
  151.         HLock(theSound);
  152.         soundDataSize = GetHandleSize(theSound) - 20L;
  153.         HUnlock(theSound);
  154.         
  155.         gTheSoundData[i] = NewPtr(soundDataSize);
  156.         if (gTheSoundData[i] == 0L)
  157.             return ResError();
  158.         HLock(theSound);
  159.         BlockMove((Ptr)(*theSound + 20L), gTheSoundData[i], soundDataSize);
  160.         HUnlock(theSound);
  161.         ReleaseResource(theSound);
  162.     }
  163.     
  164.     return theErr;
  165. }
  166.  
  167.  
  168. void waitCloseChannel(void)
  169. {
  170.     EventRecord    myEvent;
  171.     Boolean    theResult;
  172.     Boolean    channelBusy;
  173.  
  174.     channelBusy = SndChanDone(gSndChannel);
  175.     while (channelBusy)
  176.     {
  177.         theResult = WaitNextEvent(everyEvent, &myEvent, kWaitSeconds, 0L);
  178.         switch (theResult)
  179.         {
  180.             default:
  181.             {
  182.                 channelBusy = SndChanDone(gSndChannel);
  183.             }
  184.         }
  185.     }
  186.     closeSndChannel();
  187. }
  188.